﻿using Microsoft.Extensions.DependencyInjection;
using Qianke.Core.Saas.WebApi.SaasExtensions.Model;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Json;

namespace Qianke.Core.Saas.WebApi.SaasExtensions
{



    public static class NewtonsoftJsonExtensions
    {
        /// <summary>
        /// 添加 Newtonsoft.Json 序列化支持
        /// </summary>
        /// <param name="services"></param>
        public static void AddNewtonsoftJosn(this IServiceCollection services)
        {
            var preConfiguration = services.GetPreConfigureActions<IMvcBuilder>();
            preConfiguration.Add(builder =>
            {
                builder.AddNewtonsoftJson(options =>
                {
                    options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
                    options.SerializerSettings.ContractResolver = null;
                    options.SerializerSettings.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;
                    options.SerializerSettings.FloatFormatHandling = Newtonsoft.Json.FloatFormatHandling.String;
                    options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
                    //options.SerializerSettings.DefaultValueHandling = Newtonsoft.Json.DefaultValueHandling.Ignore;
                    options.SerializerSettings.Converters.Add(new IntegerToStringConverter());
                });
            });

        }

        /// <summary>
        /// 禁用 .NET Core 自动验证 ModelState 错误
        /// </summary>
        /// <param name="services"></param>
        public static void EnableNetCoreModelStateInvalid(this IServiceCollection services)
        {
            var preConfiguration = services.GetPreConfigureActions<IMvcBuilder>();
            preConfiguration.Add(builder =>
            {
                builder.ConfigureApiBehaviorOptions(options =>
                {
                    options.SuppressModelStateInvalidFilter = true;
                });
            });
        }
    }
}
